/**
 * 版权所有（c）2021. 上海海鼎信息工程股份有限公司，保留所有权利
 */
package com.lovely602.design.creation.builder.demo;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;

/**
 * @author lizhixing
 */
public class ParlourDecorator {

  public static void main(String[] args) {
    try {
      Decorator d;
      d = (Decorator) getObject();
      ProjectManager m = new ProjectManager(d);
      Parlour p = m.decorate();
      p.show();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  public static Object getObject() {
    try {
      DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = dFactory.newDocumentBuilder();
      Document doc;
      InputStream is = ClassLoader.getSystemResourceAsStream("xml/builder-config.xml");
      doc = builder.parse(is);
      NodeList nl = doc.getElementsByTagName("className");
      Node classNode = nl.item(0).getFirstChild();
      String cName = classNode.getNodeValue();
      System.out.println("新类名：" + cName);
      Class<?> c = Class.forName(cName);
      Object obj = c.newInstance();
      return obj;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}
